AC_PREREQ(2.60)
AC_INIT([Loudmouth], [1.5.3])
AC_COPYRIGHT([
        Copyright (C) 2003-2008 Imendio AB
        Copyright (C) 2007 Collabora Ltd
        Copyright (C) 2007 Nokia Corporation
])

AC_CONFIG_SRCDIR(loudmouth/loudmouth.h)
AC_CONFIG_HEADERS(config.h)
AC_CONFIG_AUX_DIR(build)
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([1.9 dist-bzip2 no-define subdir-objects -Wall])

AM_MAINTAINER_MODE

AC_CANONICAL_HOST

os_win32=no

case "$host_os" in
  *mingw32*)
    os_win32=yes
esac

if test "$os_win32" = "yes"; then
  if test "$enable_static" = "yes" -o "$enable_static" = ""; then
    AC_MSG_WARN([Disabling static library build, must build as DLL on Windows.])
    enable_static=no
  fi

  if test "$enable_shared" = "no"; then
    AC_MSG_WARN([Enabling shared library build, must build as DLL on Windows.])
  fi

  enable_shared=yes

  CFLAGS="$CFLAGS -mms-bitfields"
  LDFLAGS="$LDFLAGS -no-undefined"
fi

AC_PROG_CC([cc gcc])
AC_ISC_POSIX
AC_HEADER_STDC
dnl automake warns about not using AM_PROG_AR
dnl but automake <= 1.11 doesn't recognize it
m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL

LT_CURRENT=1
LT_REVISION=0
LT_AGE=1
AC_SUBST([LT_CURRENT])
AC_SUBST([LT_REVISION])
AC_SUBST([LT_AGE])

AM_PATH_GLIB_2_0

AC_CHECK_HEADERS([arpa/inet.h fcntl.h memory.h netdb.h netinet/in.h netinet/in_systm.h stdlib.h string.h sys/socket.h sys/time.h unistd.h])
AC_CHECK_HEADERS([winsock2.h arpa/nameser_compat.h])

if test "$ac_cv_header_winsock2_h" = "yes"; then
  # If we have <winsock2.h>, assume we find the functions
  # in -lws2_32 (ws2_32 is winsock v2, wsock32 is v1.1)
  LIBS="-lws2_32 -lgdi32 $LIBS"
fi

IDT_COMPILE_WARNINGS

changequote(,)dnl
if test "x$GCC" = "xyes"; then
  case " $CFLAGS " in
  *[\ \	]-Wall[\ \	]*) ;;
  *) CFLAGS="$CFLAGS -Wall" ;;
  esac
fi
changequote([,])dnl

AC_SUBST(CFLAGS)
AC_SUBST(LDFLAGS)

GLIB2_REQUIRED=2.12.4
GLIB2_TEST_REQUIRED=2.16.0
GNUTLS_REQUIRED=3.0.20
LIBTASN1_REQUIRED=0.2.6

AC_SUBST(GLIB2_REQUIRED)
AC_SUBST(GNUTLS_REQUIRED)
AC_SUBST(LIBTASN1_REQUIRED)

LM_CHECK_TIMEZONE

dnl Query for installation path of *.pc files, available with pkg-config>=0.27
m4_ifdef([PKG_INSTALLDIR], [PKG_INSTALLDIR])

PKG_CHECK_MODULES(LOUDMOUTH,
                  glib-2.0 >= $GLIB2_REQUIRED
                  gobject-2.0 >= $GLIB2_REQUIRED)

PKG_CHECK_MODULES(LOUDMOUTHTEST,
                  glib-2.0 >= $GLIB2_TEST_REQUIRED
                  gobject-2.0 >= $GLIB2_REQUIRED,
                  enable_test=yes, enable_test=no)
if test "$enable_test" = yes; then
  TEST_DIRS=tests
else
  TEST_DIRS=
fi
AC_SUBST([TEST_DIRS])

PKG_CHECK_MODULES(LIBIDN, libidn, have_idn=yes, have_idn=no)
if test "x$have_idn" = "xyes"; then
        AC_DEFINE(HAVE_IDN, 1, [Define if IDN support is included])
fi

dnl Gtk doc
m4_ifdef([GTK_DOC_CHECK], [
GTK_DOC_CHECK([1.14],[--flavour no-tmpl])
],[
AM_CONDITIONAL([ENABLE_GTK_DOC], false)
])

dnl define a MAINT-like variable REBUILD which is set if Perl
dnl and awk are found, so autogenerated sources can be rebuilt
AC_ARG_ENABLE(rebuilds,
              AS_HELP_STRING([--enable-rebuilds=@<:@no/yes/auto@:>@],
                             [Enable source autogeneration rules [[default=yes]]]), ,
              enable_rebuilds=yes)

REBUILD=\#
if test "x$enable_rebuilds" = "xyes" && \
     test -n "$PERL" && \
     $PERL -e 'exit !($] >= 5.002)' > /dev/null 2>&1 && \
     test -n "$AWK" ; then
  REBUILD=
fi
AC_SUBST(REBUILD)


dnl +--------------------------------------------------------+
dnl | Checking for libnsl and libsocket need on some systems |-
dnl +--------------------------------------------------------+
AC_CHECK_LIB(nsl,gethostbyname)
AC_CHECK_LIB(socket,socket)
AC_CHECK_LIB(resolv,__res_query)
AC_CHECK_LIB(resolv,res_query)

dnl +--------------------------------------------------------+
dnl | Checking for SASL GSSAPI support                       |-
dnl +--------------------------------------------------------+
AC_PATH_PROG(KRB5CONFIG, krb5-config, no)
enable_gssapi=no
if test "x$KRB5CONFIG" != "xno"
then
        AC_CHECK_HEADERS([gssapi.h gssapi/gssapi.h])
        enable_gssapi=yes
        AC_DEFINE(HAVE_GSSAPI, 1, [whether to use GSSAPI support.])
        LIBS="$LIBS `$KRB5CONFIG --libs gssapi`"
fi
AM_CONDITIONAL(USE_GSSAPI, test x$enable_gssapi = xyes)

dnl +--------------------------------------------------------+
dnl | Checking for SSL support                               |-
dnl +--------------------------------------------------------+
AC_ARG_WITH(ssl,
            AS_HELP_STRING([--with-ssl=@<:@gnutls|openssl|no@:>@],
                           [Which SSL implementation to use [[default=gnutls]]]),
            ac_ssl=$withval,
            ac_ssl=gnutls)

AC_ARG_WITH(openssl-includes,
            AC_HELP_STRING([--with-openssl-includes=DIR],[OpenSSL includes]),
            [ use_openssl=yes CPPFLAGS="$CPPFLAGS -I$withval" ]
            )

AC_ARG_WITH(openssl-libs,
            AC_HELP_STRING([--with-openssl-libs=DIR],[OpenSSL libraries]),
            [ use_openssl=yes LDFLAGS="$LDFLAGS -L$withval" ]
            )

enable_ssl=no
if test "x$ac_ssl" = "xgnutls"; then
  dnl Look for GnuTLS
  PKG_CHECK_EXISTS([gnutls >= $GNUTLS_REQUIRED], have_libgnutls=yes, have_libgnutls=no)
  if test "x$have_libgnutls" = "xyes"; then
    PKG_CHECK_MODULES(LIBGNUTLS, [gnutls >= $GNUTLS_REQUIRED])
    CFLAGS="$CFLAGS $LIBGNUTLS_CFLAGS"
    LIBS="$LIBS $LIBGNUTLS_LIBS"
    AC_DEFINE(HAVE_GNUTLS, 1, [whether to use GnuTSL support.])
    enable_ssl=GnuTLS
  else
    AC_MSG_ERROR([GnuTLS was not found, use
                --with-ssl=[[no|openssl]] to build without SSL support or
                with OpenSSL])
  fi
elif test "$ac_ssl" = "openssl"; then
  dnl Look for OpenSSL
  AC_CHECK_HEADERS([openssl/ssl.h])
  OLDLIBS="$LIBS"
  AC_CHECK_LIB(crypto,
               BIO_f_base64,
               [AC_CHECK_LIB(ssl,
                             SSL_new,
                             [SSL_LIB="-lssl -lcrypto"
                             AC_DEFINE(HAVE_OPENSSL,
                                       [],
                                       [Have OpenSSL])],
                             [have_openssl=no],
                             [-lcrypto])],
               [have_openssl=no],
               [])

  if test "x$have_openssl" = "xno"; then
    AC_MSG_ERROR([OpenSSL was not found, use --with-ssl=[[no|gnutls]] to
                  build without SSL support or with GnuTLS])
  else
    LIBS="$OLDLIBS $SSL_LIB"
    enable_ssl=OpenSSL
  fi
else
        echo "Disabling SSL support"
fi

if test x$enable_ssl != xno; then
        AC_DEFINE(HAVE_SSL, 1, [whether to use SSL support.])
else
        if test x$ac_ssl != xno; then
                AC_MSG_ERROR([No SSL implementation was found, if you do not want to build with SSL use --with-ssl=no])
        fi
fi

AM_CONDITIONAL(USE_OPENSSL, test x$enable_ssl = xOpenSSL)
AM_CONDITIONAL(USE_GNUTLS, test x$enable_ssl = xGnuTLS)

dnl +-------------------------------------------------------------------+
dnl | Checking for libasyncns                                           |
dnl +-------------------------------------------------------------------+
AC_ARG_WITH(asyncns,
        AS_HELP_STRING([--with-asyncns],
                [define whether to use libasyncns, @<:@default=no@:>@ (yes/no)]),
        ac_asyncns=$withval,
        ac_asyncns=no)

enable_asyncns=no
if test x$ac_asyncns != xno; then

        PKG_CHECK_MODULES(ASYNCNS, [libasyncns >= 0.3],
                        [enable_asyncns=yes],
                        [enable_asyncns=no])
fi

if test "x$enable_asyncns" = "xyes"; then
        AC_DEFINE(HAVE_ASYNCNS, 1, [Whether to use libasyncns])
else
        echo "Not using asynchronous dns lookups"
fi

AC_SUBST(ASYNCNS_CFLAGS)
AC_SUBST(ASYNCNS_LIBS)

dnl +-------------------------------------------------------------------+
dnl | Checking for Linux TCP/IP stack                                   |
dnl +-------------------------------------------------------------------+
AC_CHECK_DECL(TCP_KEEPCNT, [ac_use_keepalives=yes], [ac_use_keepalives=no], [
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
])

use_keepalives=no
if test x$ac_use_keepalives != xno; then
        use_keepalives=yes
        AC_DEFINE(USE_TCP_KEEPALIVES, 1, [Whether to use Linux TCP keepalives])
fi

dnl +-------------+
dnl | Build Flags |--------------------------------------------
dnl +-------------+
DOC_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $WARN_CFLAGS"
AC_SUBST(DOC_CFLAGS)
AC_SUBST(CFLAGS)
AC_SUBST(CPPFLAGS)
AC_SUBST(LDFLAGS)
dnl -----------------------------------------------------------

dnl +--------------+
dnl | Debug output |-------------------------------------------
dnl +--------------+

AC_ARG_ENABLE(debug,
              AS_HELP_STRING([--enable-debug=@<:@no/yes/auto@:>@],
                             [Enable debugging [[default=yes]]]), ,
              enable_debug=yes)

if test x$enable_debug = xno ; then
        echo "Debugging disabled"
        LOUDMOUTH_CFLAGS="$LOUDMOUTH_CFLAGS -DLM_NO_DEBUG"
else
        echo "Debugging enabled"
fi

AC_SUBST(LOUDMOUTH_CFLAGS)
AC_SUBST(LOUDMOUTH_LIBS)

AC_OUTPUT([
Makefile
docs/Makefile
docs/reference/Makefile
loudmouth/Makefile
examples/Makefile
tests/Makefile
tests/parser-tests/Makefile
loudmouth-1.0.pc])

dnl ==========================================================================
echo "

        Loudmouth $VERSION
        ==================

        prefix:                   ${prefix}
        compiler:                 ${CC}
        Have IDN support:         ${have_idn}
        Enable SSL:               ${enable_ssl}
        Asynchronous DNS:         ${enable_asyncns}
        Linux TCP keepalives:     ${use_keepalives}
        Enable Debug:             ${enable_debug}
        Enable GSSAPI:            ${enable_gssapi}
        Enable Documentation:     ${enable_gtk_doc}
        Enable Tests:             ${enable_test}

        Now type 'make' to build Loudmouth
"

